package com.jc.subject.domin.service.impl;

import com.alibaba.fastjson.JSON;
import com.jc.subject.common.enums.CategoryTypeEnum;
import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.domin.convert.SubjectLabelConverter;
import com.jc.subject.domin.entity.SubjectLabelBO;
import com.jc.subject.domin.service.SubjectLabelDomainService;
import com.jc.subject.infra.basic.entity.SubjectCategory;
import com.jc.subject.infra.basic.entity.SubjectLabel;
import com.jc.subject.infra.basic.entity.SubjectMapping;
import com.jc.subject.infra.basic.service.SubjectCategoryService;
import com.jc.subject.infra.basic.service.SubjectLabelService;
import com.jc.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.EnumerationPropertyAccessor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: ASL_ly
 * @Package: com.jc.subject.domin.service.impl
 * @Project: jc-club
 * @Date: 2024/10/22  22:35
 */
@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 新增标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.add.BO:{}",JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter
                .INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
       Integer count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    /**
     * 修改标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.update.BO:{}",JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter
                .INSTANCE.convertBoToLabel(subjectLabelBO);
        Integer count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 删除标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.delete.BO:{}",JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter
                .INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.code);
        Integer count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 根据分类id查询标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("subjectLabelController.queryLabelByCategoryId.BO:{}",JSON.toJSONString(subjectLabelBO));
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        //如果当前分类是一级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(categoryId);
        if(subjectCategory.getCategoryType() == CategoryTypeEnum.PRIMARY.code){
            SubjectLabel sl = SubjectLabel.builder()
                    .categoryId(categoryId)
                    .isDeleted(IsDeletedFlagEnum.UN_DELETED.code)
                    .build();
            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(sl);
            List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter
                    .INSTANCE.convertBoLabelToBOList(labelList);
            return subjectLabelBOList;
        }
        //如果当前分类是二级分类
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        //1.category_mapping表查询categoryId下的所有标签id
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelIdList(subjectMapping);

        //2.标签id查询结果为空，直接返回空集
        if(CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }

        //3.根据标签id在category_label表查询标签id对应标签信息
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList = subjectLabelService.batchQuerryById(labelIdList);
        List<SubjectLabelBO> subjectLabelBOList = new LinkedList<>();
        subjectLabelList.forEach(label ->{
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setSortNum(label.getSortNum());
            bo.setCategoryId(categoryId);
            subjectLabelBOList.add(bo);
        });

        return subjectLabelBOList;
    }
}
